package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.BindView;
import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseFragment;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.AdvancesOnDoInfo;
import com.tiza.xiaoyoubao.entity.BrowseOnDoInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/3/5.
 * 浏览任务已撤销
 */

public class BrowseOnDoFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private List<BrowseOnDoInfo> infoList;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new BrowseOnDoAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;

    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        BrowseOnDoInfo info = (BrowseOnDoInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.getCancelOrderList(this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "002","7",
                new JsonCallback<BrowseOnDoInfo>(BrowseOnDoInfo.class) {

                    @Override
                    public void onError(Call call, Exception e) {
                        onRequestError();
                    }

                    @Override
                    public void onResponse(
                            ResponseInfo<BrowseOnDoInfo> response
                    ) {
                        int code = response.getCode();
                        if (code == 1) {
                            mErrorLayout.setErrorType(
                                    EmptyLayout.NETWORK_ERROR);
                            return;
                        }
                        infoList = response.getData();
                        if (infoList!=null) {
                            Collections.sort(infoList,
                                    new Comparator<BrowseOnDoInfo>() {
                                        @Override
                                        public int compare(
                                                BrowseOnDoInfo browseOnDoInfo,
                                                BrowseOnDoInfo t1
                                        ) {
                                            return (t1
                                                    .getAddtime())
                                                    .compareTo(
                                                            (browseOnDoInfo.getAddtime()));
                                        }
                                    }
                            );
                        }
                        onRequestResponse(infoList);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        onRequestFinish();
                    }
                }
        );
    }

    public static class BrowseOnDoAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_browse_ondo_fragment,
                    parent, false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(
                View view, int viewType
        ) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh =(ViewHolder) holder;
            BrowseOnDoInfo info = (BrowseOnDoInfo) _data.get(position);
            vh.tv_shopName.setText(info.getAccount());
            vh.tv_orderId.setText("订单编号:" + info.getOrderid());
            vh.tv_time.setText("撤销时间:" + info.getAddtime());
            vh.tv_cause.setText("撤销原因:" + info.getReason());

        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

            @BindView(R.id.tv_orderId)
            TextView tv_orderId;
            @BindView(R.id.tv_time)
            TextView tv_time;
            @BindView(R.id.tv_cause)
            TextView tv_cause;
            @BindView(R.id.tv_shopName)
            TextView tv_shopName;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }

    }


}
